package com.jushu.storbox.util;

import java.io.File;
import java.io.FileFilter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.os.Environment;
import android.os.storage.StorageManager;

import com.huawei.sharedrive.sdk.android.util.StringUtil;

public class DeviceUtil {

	public final static String CAMERA_DIRECTORY = "DCIM/Camera";
	public static String SDCARD_DRIECTORY = "";
	
	private static DeviceUtil deviceUtil = null;

	public static DeviceUtil instance() {
		if(deviceUtil == null) {
			deviceUtil = new DeviceUtil();
		}
		return deviceUtil;
	}
	
	List<File> storageList = new ArrayList<File>();
	
	private DeviceUtil() {
		
	}
	
	public File getCameraFolder() {
		File cameraFolder = null;
		File sdcard = Environment.getExternalStorageDirectory();
		if(sdcard != null) {
			cameraFolder = new File(sdcard, CAMERA_DIRECTORY);
			if(StringUtil.isBlank(SDCARD_DRIECTORY)) {
				SDCARD_DRIECTORY = sdcard.getAbsolutePath();
			}
		}
		return cameraFolder;
	}
	
	public List<File> getStorageList(Context context ) {
		List<File> storageList = new ArrayList<File>();
//		SDCardFilter fileter = new SDCardFilter();
		HashMap<String, File> hashMap = new HashMap<String, File>();
		
		StorageManager storagemanager = (StorageManager)context.getSystemService(Context.STORAGE_SERVICE);
		try {
			Method getVolumePathsMethod = storagemanager.getClass().getMethod("getVolumePaths");
			String[] paths = (String[])getVolumePathsMethod.invoke(storagemanager);
			for(String temp : paths) {
				hashMap.put(temp, new File(temp));
			}
		} catch (NoSuchMethodException e) {		//no such method  getVolumePaths
			// TODO Auto-generated catch block
			File defaultSDCard = Environment.getExternalStorageDirectory();
			if(defaultSDCard != null) {
				hashMap.put(defaultSDCard.getAbsolutePath(), defaultSDCard);
			}
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		if(!hashMap.isEmpty()) {
			for(String mapItem : hashMap.keySet()) {
				File item = hashMap.get(mapItem);
				File[] tempList = item.listFiles();
				if(tempList != null && tempList.length > 0) {
					storageList.add(item);
				}
			}
		}
		
		return storageList;
	}
	
	private void findMountSdCard(File targetFolder, FileFilter fileter, HashMap<String, File> storageMap) {
		if(targetFolder != null && targetFolder.exists()) {
			File[] files = targetFolder.listFiles(fileter);
			if(files != null) {
				for(File f : files) {
					storageMap.put(f.getPath(), f);
				}
			}
		}
	}

	
	/** 
     * @return 
     */  
    public boolean hasSDCard(){  
        boolean b = false;  
        if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){  
            b = true;  
        }  
        return b;  
    }  
      
    /** 
     * get sdcard path
     * @return 
     */  
    public String getExtPath(){  
        String path = "";  
        if(hasSDCard()){  
            path = Environment.getExternalStorageDirectory().getPath();  
        }  
        return path;  
    }  
}
